﻿using SolidWorks.Interop.sldworks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SolidworksDLL
{
    public partial class TestFrm : Form
    {
        public TestFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool b = SolidWorksHelp.GetSwAPP();
            if (b)
            {
                MessageBox.Show("获取成功");
            }
            else
            {
                MessageBox.Show("获取失败");
                return;                    
            }
            string AsmPath = OpenAsm();
            if (AsmPath=="")
            {
                return;
            }
            richTextBox1.Text = "选择的文件："+AsmPath;
            //关闭所有打开的文件
            SolidWorksHelp.swApp.CloseAllDocuments(true);

            ModelDoc2 swModel = null;
            richTextBox1.AppendText(System .Environment .NewLine +"打开装配体文件");
            string result = ModelDocHelp.OpenDoc(AsmPath ,ref swModel);
            if (swModel ==null)
            {
                MessageBox.Show("打开装配体失败"+result );
                return;
            }
            AssemblyDoc asmDoc = ModelDocHelp.GetAsm(swModel);
            if (null == asmDoc)
            {

                return;
            }
            richTextBox1.AppendText(System.Environment.NewLine + "获取装配体文件所有子件对象");
            //获取子零部件
            int count = asmDoc.GetComponentCount(true);
            if (count == 0)
            {
                return;
            }
            object[] components = (object[])asmDoc.GetComponents(true);
            foreach (Component2 item in components)
            {
                bool b1 = item.IsEnvelope(); //封套
                bool b2 = item.IsSuppressed();//压缩
                int v = item.Visible; //可见性
                string name = item.Name2;//名称
                string path = item.GetPathName(); //路径
                richTextBox1.AppendText(System.Environment.NewLine + "子件名称：" + name + ",路径：" + path);
            }

            //获取零部件的依赖
            richTextBox1.AppendText(System.Environment.NewLine + "获取零部件参考信息");
            object[] depends = (object[])SolidWorksHelp.swApp.GetDocumentDependencies2(AsmPath, true, true, false);

            if (depends == null)
            {
                return;
            }

            for (int i = 0; i < depends.Length; i = i + 2)
            {
                string name = (string)depends[i];//名称
                string path = (string)depends[i + 1]; //路径

                richTextBox1.AppendText(System.Environment.NewLine + "参考名称：" + name + ",路径：" + path);
            }


            List<string> modelPaths = ModelDocHelp.GetAllDoc();

            richTextBox1.AppendText(System.Environment.NewLine + "非还原状态所有打开文件数量：" + modelPaths.Count.ToString());
            //轻化到还原状态
            asmDoc.ResolveAllLightweight();
            modelPaths = ModelDocHelp.GetAllDoc();

            richTextBox1.AppendText(System.Environment.NewLine + "还原状态所有打开文件数量：" + modelPaths.Count.ToString());




        }
        private string OpenAsm()
        {
            openFileDialog1.Filter = "Sildworks装配体文件|*.sldasm";
            openFileDialog1.FileName = "";
            var result = openFileDialog1.ShowDialog();
            if (result==DialogResult.OK)
            {
                return openFileDialog1.FileName;
            }
            return "";                        
        }
    }
}
